package com.kanesoft.android.gdx.widgets.sprites;

import java.util.Iterator;
import java.util.Vector;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class SpriteInfo {
    
    private Vector<SpriteSlideInfo> slidesList;
    private Vector<TextureRegion> regionsList;
    private Vector<TextureRegion> attackList;
    
    private float imageFrameSleepTime;
    private Texture imagesFileTexture;
    
    public SpriteInfo(String imageFileName, float frameDelay) {
        slidesList = new Vector<SpriteSlideInfo>();
        imagesFileTexture = new Texture(Gdx.files.internal(imageFileName));
        setframeDelay(frameDelay);
    }
    
    /**
     * Initialize SpriteInfo using a previous SpriteInfo to reuse Texture.
     * This saves memory and time on multiple movements animations.
     * @param reusedSpriteInfo
     */
    public SpriteInfo(SpriteInfo reusedSpriteInfo, float frameDelay) {
        slidesList = new Vector<SpriteSlideInfo>();
        imagesFileTexture = reusedSpriteInfo.imagesFileTexture;
        setframeDelay(frameDelay);
    }


    public void addSpriteSlide(SpriteSlideInfo slideInfo) {
        slidesList.add(slideInfo);
    }
    
    public Animation getSpriteAnimation(boolean flipHorizontal, boolean flipVertical) {
        regionsList = new Vector<TextureRegion>();
        attackList = new Vector<TextureRegion>();
        
        for (Iterator<SpriteSlideInfo> i = slidesList.iterator(); i.hasNext();) {
            SpriteSlideInfo slideInfo = i.next();
            
            TextureRegion slideRegion = new TextureRegion(imagesFileTexture, slideInfo.imagePosXInTheImageFile, 
                    slideInfo.imagePosYInTheImageFile, slideInfo.imageWidthInTheImageFile, slideInfo.imageHeightInTheImageFile);
            slideRegion.flip(flipHorizontal, flipVertical);
            regionsList.add(slideRegion);
            if (slideInfo.imageAttackSlide == true) attackList.add(slideRegion);
        }
        
        return new Animation(imageFrameSleepTime, regionsList /*new TextureRegion(testTexture, 0, 0, 50, 50)*/);
    }
    
    public TextureRegion lastTestedAttackRegion;
    public boolean lastTestedAttackValue;
    
    public boolean isAttackFrame(TextureRegion currentTexture) {
        if (attackList.size() == 0) return false;
        
        if ((lastTestedAttackRegion != null) && (lastTestedAttackRegion.equals(currentTexture))) {
            return lastTestedAttackValue;
        }
        
        if (attackList.contains(currentTexture)) {
            return true;
        }
        
        return false;
    }
    
    public float getAnimationDuration() {
        return (float) slidesList.size() * imageFrameSleepTime;
    }
    
    public void setframeDelay(float frameDelay) {
        imageFrameSleepTime = frameDelay;
        
    }
    
}
